package com.offcn.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.offcn.mapper.MemberMapper;
import com.offcn.mapper.OrderMapper;
import com.offcn.mapper.SetmealMapper;
import com.offcn.pojo.Member;
import com.offcn.pojo.Order;
import com.offcn.service.ReportService;
import com.offcn.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wy
 * @Date: 2022/9/5 14:19
 */
@Service
@org.springframework.stereotype.Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //1 获取当前日期
        String reportDataday = DateUtils.parseDate2String(new Date());

        //存放数据的map
        HashMap<String, Object> map = new HashMap<>();

        map.put("reportDate",reportDataday);


        //2 获取当日新增会员数 存放到 todayNewMember 在 member表中 查
        LambdaQueryWrapper<Member> memberLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        memberLambdaQueryWrapper1.eq(Member::getRegtime, reportDataday);
        Integer todayNewMember = memberMapper.selectCount(memberLambdaQueryWrapper1);
        map.put("todayNewMember", todayNewMember);

        //3 查询总会员数 存放到 totalMember
        Integer totalMember = memberMapper.selectCount(null);
        map.put("totalMember", totalMember);

        //4 查询本周新增会员数  存放到 thisWeekNewMember
        //4.1 先去获取本周 第一天
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //4.2 获取到之后 我们 在去判断 大于 等于 这一天的会员数
        LambdaQueryWrapper<Member> memberLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        memberLambdaQueryWrapper2.ge(Member::getRegtime, thisWeekMonday);
        Integer thisWeekNewMember = memberMapper.selectCount(memberLambdaQueryWrapper2);
        map.put("thisWeekNewMember", thisWeekNewMember);

        //5： 获取本月新增会员数
        //5。1 获取 本月 第一天的时间  存放——thisMonthNewMember
        String forstMonthMonday = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //5.2  获取到之后 我们 在去判断 大于 等于 这一天的会员数
        LambdaQueryWrapper<Member> memberLambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        memberLambdaQueryWrapper3.ge(Member::getRegtime, forstMonthMonday);
        Integer thisMonthNewMember = memberMapper.selectCount(memberLambdaQueryWrapper3);
        map.put("thisMonthNewMember", thisMonthNewMember);

        //6: 获取 今日预约数 todayOrderNumber ---表格 order
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper1.eq(Order::getOrderdate, reportDataday);
        Integer todayOrderNumber = orderMapper.selectCount(orderLambdaQueryWrapper1);
        map.put("todayOrderNumber", todayOrderNumber);

        //7： 获取今日 到 珍数  todayVisitsNumber
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper2.ge(Order::getOrderdate, reportDataday).eq(Order::getOrderstatus, "已到诊");
        Integer todayVisitsNumber = orderMapper.selectCount(orderLambdaQueryWrapper2);
        map.put("todayVisitsNumber", todayVisitsNumber);


        //8： 获取本周预约数 thisWeekOrderNumber
        //新获取本周 第一天
        //根据天数在进行判断
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper3.ge(Order::getOrderdate, thisWeekMonday);
        Integer thisWeekOrderNumber = orderMapper.selectCount(orderLambdaQueryWrapper3);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);


        //9： 获取本周 到 珍数  thisWeekVisitsNumber
        //在进行判断
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper4 = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper4.ge(Order::getOrderdate, thisWeekMonday).eq(Order::getOrderstatus, "已到诊");
        Integer thisWeekVisitsNumber = orderMapper.selectCount(orderLambdaQueryWrapper4);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);


        //10： 本月 预约数  thisMonthOrderNumber

        //获取 本月第一天

        //在进行判断
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper5 = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper5.ge(Order::getOrderdate, forstMonthMonday);
        Integer thisMonthOrderNumber = orderMapper.selectCount(orderLambdaQueryWrapper5);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);


        //11： 本月 到珍数 thisMonthVisitsNumber
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper6 = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper6.ge(Order::getOrderdate, forstMonthMonday).eq(Order::getOrderstatus, "已到诊");
        Integer thisMonthVisitsNumber = orderMapper.selectCount(orderLambdaQueryWrapper6);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);


        //查询 热门套餐 hotSetmeal
        List<Map<String, Object>> hotSetmeal = setmealMapper.findHotSetmeal();

        map.put("hotSetmeal", hotSetmeal);
        return map;
    }
}
